package com.sendi.common.util;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class Page implements Serializable {
	private static final long serialVersionUID = 1L;
	private static final int DEFAULT_PAGE_SIZE = 20;
	private int pageSize;
	private long start;
	private List data;
	private long totalCount;
	private List results;

	public Page(){
		this(0L, 0L, 20, new ArrayList());
	}
	public Page(long start, long totalSize, int pageSize, List data){
	    this.pageSize = pageSize;
	    this.start = start;
	    this.totalCount = totalSize;
	    this.data = data;
	    results = data;
	}
	public Page(long start, long totalSize, int pageSize, List data,boolean prepareData){
	    this.pageSize = pageSize;
	    this.start = start;
	    this.totalCount = totalSize;
	    this.data = data;
	    if(prepareData)
	    	results = prepareDate(start,pageSize,data);
	    else
	    	results = data;
	}
	public List prepareDate(long start,long pageSize,List data){
		if(pageSize == 0
				|| data == null 
				|| data.size() == 0
				|| data.size() <= start
				|| data.size() <= pageSize)
			return data;
		// 删除前面数据
		for(int i=0;i<start;i++){
			data.remove(0);
		}
		// 删除后面数据
		while(data.size()>pageSize){
			data.remove(data.size()-1);
		}
		return data;
	}

	public long getTotalCount(){
		return this.totalCount;
	}

	public long getTotalPageCount(){
		if (this.totalCount % this.pageSize == 0L) {
			return (this.totalCount / this.pageSize);
		}
		return (this.totalCount / this.pageSize + 1L);
	}

	public int getPageSize(){
		return this.pageSize;
	}

	public List getResult(){
		return this.data;
	}

	public void setResult(List list){
		this.data = list;
	}

	public long getCurrentPageNo(){
		return (this.start / this.pageSize + 1L);
	}

	public boolean hasNextPage(){
		return (getCurrentPageNo() < getTotalPageCount() - 1L);
	}

	public boolean hasPreviousPage(){
		return (getCurrentPageNo() > 1L);
	}

	protected static int getStartOfPage(int pageNo){
		return getStartOfPage(pageNo, 20);
	}

	public static int getStartOfPage(int pageNo, int pageSize){
		return ((pageNo - 1) * pageSize);
	}
	public void setTotalCount(long totalCount) {
		this.totalCount = totalCount;
	}
	public List getResults() {
		return results;
	}
	public void setResults(List results) {
		this.results = results;
	}
}
